import { IColumn } from "../interfaces";
import * as Types from "./types";
import { IAction } from "../../../../common/store/actions";

export interface IReducerState {
  table_datasource: { total: number; list: Array<IColumn> };
  table_loading: boolean;
  table_filter: IReducerState_TableFilter;
  form_info: IReducerState_Form;
}

export interface IReducerState_Form {
  visible_add: boolean;
  loading_add: boolean;
  visible_edit: boolean;
  echo_edit: IColumn | null;
  loading_edit: boolean;
}

export interface IReducerState_TableFilter {
  pageNum: number;
  pageSize: number;
  sortColumn: null | string;
  sortType: null | string;
  selectedKeys: Array<number> | Array<string>;
  keyWords: null | string;
  ownerId: undefined | number;
  type: undefined | number;
}

export const defaultState: IReducerState = {
  table_datasource: { total: 0, list: Array<IColumn>() },
  table_loading: false,
  table_filter: {
    pageNum: 1,
    pageSize: 10,
    sortColumn: null,
    sortType: null,
    selectedKeys: Array<number>(),
    keyWords: null,
    ownerId: undefined,
    type: undefined
  },
  form_info: {
    visible_add: false,
    visible_edit: false,
    echo_edit: null,
    loading_add: false,
    loading_edit: false
  }
};

export default (state = defaultState, action: IAction): typeof defaultState => {
  switch (action.type) {
    case Types.TABLE.DATASOURCE: {
      return { ...state, table_datasource: action.data };
    }
    case Types.TABLE.FILTER: {
      return { ...state, table_filter: action.data };
    }
    case Types.TABLE.LOADING: {
      return { ...state, table_loading: action.data };
    }
    case Types.FORM: {
      return { ...state, form_info: action.data };
    }
    default: {
      return state;
    }
  }
};
